package com.rpframework.website.device.service;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.rpframework.core.BaseService;
import com.rpframework.core.api.FileService;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
/**
 * @author zl
 * 处理多个图片对象
 * */
@Service
public class ImageService extends BaseService{
	@Resource FileService fileService;
	
	public String uploadImg(MultipartFile file, String name) {
		StringBuilder all = new StringBuilder();
		try {
			StringBuilder str = new StringBuilder();
			str.append("/device/" + name + "/" + DateUtils.nowDate(DateUtils.YYYYMMDDHHMMSS) +  NumberUtils.random() + "." + FilenameUtils.getExtension(file.getOriginalFilename()));
			this.fileService.saveFile(file.getInputStream(), str.toString());
			all.append(str);
		} catch (Exception e) {
			throw new IllegalArgumentException("文件上传失败，原因:" + e.getLocalizedMessage());
		}
		return all.toString();
	}
}
